<?php

// 参考：http://www.php.net/manual/zh/pdo.connections.php
// https://segmentfault.com/a/1190000004106175

include_once 'db_config.php';
try {
// array(PDO::ATTR_PERSISTENT => true) 开启持久化连接
    $dbh = new PDO('mysql:host=localhost;dbname=wx', $db['user'], $db['pwd'], array(PDO::ATTR_PERSISTENT => true));

    if (empty($_POST["date_start"]) & empty($_POST["date_end"])) {
        // 获取指定日期的净增用户总量
        $next_day = date("Y-m-d", strtotime('-7 days'));
        $end_day = date('Y-m-d', strtotime('-1 days'));
//    echo $next_day;
//    echo $end_day;        
    } else {
        $next_day = $_POST['date_start'];
        $end_day = $_POST['date_end'];
//    echo $next_day;
//    echo $end_day;        
    }

    while (strtotime($next_day) <= strtotime($end_day)) {
        $sql = 'SELECT sum(int_page_read_user) as iru,sum(int_page_read_count) as irc,sum(ori_page_read_user)as oru,sum(ori_page_read_count) as orc,sum(share_user) as su,sum(share_count) as sc,sum(add_to_fav_user) as afu,sum(add_to_fav_count) as afc FROM `userread` where ref_date="' . $next_day . '" ';
        foreach ($dbh->query($sql) as $row) {
            // 把日期和用户数单独组成列 | 变量追加到数组中
            $arr['date'][] = $next_day;
            $arr['iru'][] = $row['iru'];
//            $arr['irc'][] = $row['irc'];
            $arr['oru'][] = $row['oru'];
//            $arr['orc'][] = $row['orc'];
            $arr['su'][] = $row['su'];
//            $arr['sc'][] = $row['sc'];
            $arr['afu'][] = $row['afu'];
//            $arr['afc'][] = $row['afc'];
        }
        $next_day = date("Y-m-d", strtotime("+1 days", strtotime($next_day)));
    }

    // 把数据转换成json格式
    $data = json_encode($arr);
    echo $data;

    // 现在运行完成，在此关闭连接
    $dbh = null;
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}
?>